package com.example.mybaiduweishi.activity;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.mybaiduweishi.MyApplication;
import com.example.mybaiduweishi.R;
import com.example.mybaiduweishi.activity.base.BaseActivity;
import com.example.mybaiduweishi.services.IVersionUpdate;
import com.example.mybaiduweishi.utils.MyUtil;
import com.example.mybaiduweishi.vo.VersionInfo;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class Update_banbenActivity extends BaseActivity {

    final String TAG = "Zhu//";

    @BindView(R.id.pic_img)
    ImageView picImg;
    @BindView(R.id.dialog_text)
    TextView dialogText;
    @BindView(R.id.pic_one)
    ImageView picOne;
    private StringBuffer string = new StringBuffer();
    private boolean aBoolean;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
    private void update() {
        //1.连接服务器,查看是否有最新版本,  联网操作,耗时操作,4.0以后不允许在主线程中执行的,放到子线程中执行
        new Thread(){
            public void run() {
                try {
                    //1.1连接服务器
                    //1.1.1设置连接路径
                    //spec:连接路径
                    URL url = new URL("http://192.168.1.124:8080");
                    //1.1.2获取连接操作
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();//http协议,httpClient
                    //1.1.3设置超时时间
                    conn.setConnectTimeout(5000);//设置连接超时时间
                    //conn.setReadTimeout(5000);//设置读取超时时间
                    //1.1.4设置请求方式
                    conn.setRequestMethod("GET");//post
                    //1.1.5获取服务器返回的状态码,200,404,500
                    int responseCode = conn.getResponseCode();
                    if (responseCode == 200) {
                        //连接成功
                    }else{
                        //连接失败
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            };
        }.start();
    }
    @Override
    public int getContentXML() {
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        return R.layout.activity_update_banben;
    }

    @Override
    public void initData() {
        //检测网络是否连通
        boolean b_check = checkNet();

        if (b_check) {
            //访问服务器  获取Json字段判断版本问题
            //1、表示无网 2、有网但网速慢 3、网络畅通，可以下载 4、最新版本无需更新

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("http://10.0.2.2:8080/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            IVersionUpdate versionUpdate = retrofit.create(IVersionUpdate.class);  //创建接口对象
            Call<VersionInfo> versionInfoCall = versionUpdate.getVersion();
            versionInfoCall .enqueue(new Callback<VersionInfo>() {
                @Override
                public void onResponse(Call<VersionInfo> call, Response<VersionInfo> response) {
                    //网络正常
                    VersionInfo versionInfo = response.body();
                    int code = versionInfo.getVersioncode();    //最新的code
                    int oldcode = MyUtil.getVersionCode(MyApplication.getContext());
                    if (code>oldcode){
                        Intent intent = new Intent();
                        //访问服务器的实体类
                        intent.putExtra("VersionInfo",response.body());  //body是实体类，在网络中的传输需要实体化
                        setResult(3,intent);
                        finish();
                    }else {
                        setResult(4);
                        finish();
                    }
                }

                @Override
                public void onFailure(Call<VersionInfo> call, Throwable t) {
                    //网络缓慢
                    setResult(2);
                    finish();
                }
            });
        } else {
            setResult(1);   //传值方法
            finish();
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

    private boolean checkNet() {
        NetworkInfo.State wifiState = null;
        NetworkInfo.State mobileState = null;
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        if (wifiState != null && mobileState != null && NetworkInfo.State.CONNECTED != wifiState
                && NetworkInfo.State.CONNECTED == mobileState) {
            // 手机网络连接成功
            Toast.makeText(this, "手机网络连接成功", Toast.LENGTH_SHORT).show();
            return true;

        } else if (wifiState != null && mobileState != null
                && NetworkInfo.State.CONNECTED != wifiState
                && NetworkInfo.State.CONNECTED != mobileState) {
            // 手机没有任何的网络
            Toast.makeText(this, "手机没有任何的网络", Toast.LENGTH_SHORT).show();
            return false;
        } else if (wifiState != null && NetworkInfo.State.CONNECTED == wifiState) {
            // 无线网络连接成功
            Toast.makeText(this, "无线网络连接成功", Toast.LENGTH_SHORT).show();
            return true;
        }
        return false;
    }


    @Override
    public void initView() {
        final AnimatorSet animatorSet=new AnimatorSet();
        ObjectAnimator scaleX1=ObjectAnimator.ofFloat(picOne,"scaleX", 1f, 0f);
        ObjectAnimator scaleY1=ObjectAnimator.ofFloat(picOne,"scaleY", 1f, 0f);
        ObjectAnimator scaleX2=ObjectAnimator.ofFloat(picOne,"scaleX", 0f, 1f);
        ObjectAnimator scaleY2=ObjectAnimator.ofFloat(picOne,"scaleY", 0f, 1f);
        string.append(".");
        dialogText.setText("Loading"+ string.toString());
        animatorSet.setDuration(2000);
        animatorSet.play(scaleX2).with(scaleY2).before(scaleX1).before(scaleY1);//两个动画同时开始
        animatorSet.start();
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                picImg.setEnabled(aBoolean);
                aBoolean=!aBoolean;
                string.append(".");
                if(string.toString().length()>3){
                    Toast.makeText(Update_banbenActivity.this, TAG, Toast.LENGTH_SHORT).show();
                    string=new StringBuffer();
                }
                dialogText.setText("Loading"+ string.toString());
                ObjectAnimator scaleX1=ObjectAnimator.ofFloat(picImg,"scaleX", 1f, 0f);
                ObjectAnimator scaleY1=ObjectAnimator.ofFloat(picImg,"scaleY", 1f, 0f);
                ObjectAnimator scaleX2=ObjectAnimator.ofFloat(picImg,"scaleX", 0f, 1f);
                ObjectAnimator scaleY2=ObjectAnimator.ofFloat(picImg,"scaleY", 0f, 1f);
                animatorSet.setDuration(2000);
                animatorSet.play(scaleX2).with(scaleY2).before(scaleX1).before(scaleY1);//两个动画同时开始
                animatorSet.start();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
    }
}